package br.com.miboco.patterns.template.post;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;

public class TextAreaPostComponent extends PostTemplateComponent {
	public Widget getTypeArea() {
		final TextArea widgetArea = new TextArea();
		
		widgetArea.setTitle("new post");
		widgetArea.setText("hello people, type here anything ...");
		widgetArea.setSize("352px", "83px");

		widgetArea.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				onClickTextArea(widgetArea,event);
			}
		});

		widgetArea.addBlurHandler(new BlurHandler() {
			public void onBlur(BlurEvent event) {
				addOnBlur(widgetArea,event);
			}
		});
		
		widgetArea.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				addKeyTextArea(widgetArea,event);
				
			}
		});
		
		return widgetArea;
	}

	
	protected void addKeyTextArea(TextArea widgetArea, KeyUpEvent event) {
		setLastMessage( widgetArea.getText() );
		updateLabelMaxTyped( getLastMessage().length() );
	}
	
	protected void onClickTextArea(final TextArea widgetArea,final ClickEvent event) {
		if(!isChange())
			widgetArea.setText("");
	}

	protected void addOnBlur(final TextArea widgetArea,BlurEvent event) {
		if (widgetArea.getText().length() == 0 && !isChange())
			widgetArea.setText("hello people, type here anything :) ...");

	}
}
